/**
 * 购物车数据结构测试示例
 */

// 购物车form数据结构示例
const cartFormExample = [
    {
        cart_id: 123,           // 购物车记录ID
        goods_id: 456,          // 商品ID
        sku_id: 789,            // SKU ID
        quantity: 2,             // 商品数量
        selected: true,          // 是否选中
        goods_name: "iPhone 15 Pro",  // 商品名称
        sku_specs: "深空黑色@256GB",   // SKU规格
        sku_price: 8999.00,     // SKU价格
        goods_image: "https://example.com/iphone15pro.jpg"  // 商品图片
    },
    {
        cart_id: 124,
        goods_id: 457,
        sku_id: 790,
        quantity: 1,
        selected: true,
        goods_name: "AirPods Pro",
        sku_specs: "白色@标准版",
        sku_price: 1999.00,
        goods_image: "https://example.com/airpodspro.jpg"
    }
];

// 模拟数量更改时的数据更新
function updateQuantity(formData, index, newQuantity) {
    if (index >= 0 && index < formData.length) {
        // 创建新的数组，避免直接修改原数据
        const newForm = [...formData];
        newForm[index].quantity = newQuantity;
        
        // 重新计算总价
        const totalPrice = newForm
            .filter(item => item.selected)
            .reduce((total, item) => total + (item.sku_price * item.quantity), 0);
        
        return {
            form: newForm,
            totalPrice: totalPrice.toFixed(2)
        };
    }
    return null;
}

// 模拟商品选择状态更改
function toggleItemSelection(formData, index) {
    if (index >= 0 && index < formData.length) {
        const newForm = [...formData];
        newForm[index].selected = !newForm[index].selected;
        
        // 检查是否全选
        const isAllSelected = newForm.every(item => item.selected);
        
        // 重新计算总价和选中数量
        const selectedItems = newForm.filter(item => item.selected);
        const totalPrice = selectedItems.reduce((total, item) => total + (item.sku_price * item.quantity), 0);
        
        return {
            form: newForm,
            isAllSelected,
            totalPrice: totalPrice.toFixed(2),
            selectedCount: selectedItems.length
        };
    }
    return null;
}

// 测试数量更改
console.log('原始数据:', cartFormExample);
const updatedData = updateQuantity(cartFormExample, 0, 3);
console.log('数量更改后:', updatedData);

// 测试商品选择
const selectionData = toggleItemSelection(cartFormExample, 1);
console.log('选择状态更改后:', selectionData);

// 获取选中商品列表（用于提交订单）
function getSelectedItems(formData) {
    return formData.filter(item => item.selected);
}

// 构建订单数据
function buildOrderData(formData, address, pickUpPoint, expressType) {
    const selectedItems = getSelectedItems(formData);
    const totalPrice = selectedItems.reduce((total, item) => total + (item.sku_price * item.quantity), 0);
    
    return {
        items: selectedItems,
        address: expressType === 'express' ? address : null,
        pickUpPoint: expressType === 'pickUpPoint' ? pickUpPoint : null,
        expressType: expressType,
        totalPrice: totalPrice.toFixed(2)
    };
}

// 测试订单数据构建
const orderData = buildOrderData(
    cartFormExample,
    { name: '张三', phone: '13800138000', address: '北京市朝阳区...' },
    { name: '自提点A', address: '北京市朝阳区...' },
    'pickUpPoint'
);

console.log('订单数据:', orderData);

module.exports = {
    cartFormExample,
    updateQuantity,
    toggleItemSelection,
    getSelectedItems,
    buildOrderData
};
